#include "multipath.h"
#include "math.h"
#include "dac.h"
#include "ad9959.h"

const uint16_t ad603_table[11][10] = {
{1080, 1172, 1221, 1264, 1295, 1320, 1337, 1356, 1369, 1383},//30MHz
{1084, 1176, 1224, 1267, 1299, 1324, 1341, 1359, 1371, 1386},//31MHz
{1087, 1179, 1227, 1271, 1303, 1327, 1343, 1360, 1374, 1388},//32MHz
{1090, 1182, 1230, 1274, 1306, 1330, 1346, 1363, 1378, 1391},//33MHz
{1094, 1185, 1234, 1277, 1309, 1333, 1349, 1366, 1381, 1394},//34MHz
{1098, 1188, 1237, 1280, 1312, 1336, 1352, 1369, 1384, 1396},//35MHz
{1101, 1191, 1241, 1284, 1315, 1339, 1355, 1372, 1387, 1399},//36MHz
{1104, 1193, 1243, 1287, 1318, 1341, 1357, 1375, 1390, 1403},//37MHz
{1107, 1196, 1245, 1290, 1320, 1344, 1360, 1377, 1393, 1405},//38MHz
{1110, 1199, 1248, 1294, 1323, 1347, 1363, 1380, 1394, 1408},//39MHz
{1114, 1202, 1253, 1297, 1326, 1349, 1366, 1383, 1397, 1411},//40MHz
};

float convert_dB_to_multiple(char dB){
	return pow(10,dB/20.0);
}

void ad9959_change_mod_signal_freq(uint16_t mod_signal_freq_MHz){
	ad9959_write_frequency(AD9959_CHANNEL_1, mod_signal_freq_MHz*1000000);//直达调制信号
	ad9959_write_frequency(AD9959_CHANNEL_3, mod_signal_freq_MHz*1000000);//多径调制信号
}

void ad9959_change_carrier_wave_freq(uint16_t carrier_wave_freq_MHz){
	ad9959_write_frequency(AD9959_CHANNEL_0, carrier_wave_freq_MHz*1000000);//直达载波
	ad9959_write_frequency(AD9959_CHANNEL_2, carrier_wave_freq_MHz*1000000);//多径载波
}

void ad9959_change_mod_signal_amp(uint16_t Ma_percent){
	ad9959_write_amplitude(AD9959_CHANNEL_1, (uint16_t)(Ma_percent/94.0*1023 + 0.5));//=Ma*1023
	ad9959_write_amplitude(AD9959_CHANNEL_3, (uint16_t)(Ma_percent/94.0*1023 + 0.5));
}

void ad603_change_carrier_wave_amp(uint16_t amp_effective_mV, uint16_t carrier_wave_freq_MHz, uint16_t Ma_percent){
	uint16_t ad603_value;
	if(Ma_percent > 5 && amp_effective_mV >= 600){
		uint16_t Ma_min = 30, Ma_max = 90;
		uint16_t y_min = ad603_table[carrier_wave_freq_MHz-30][900/100 - 1], y_max = ad603_table[carrier_wave_freq_MHz-30][600/100 - 1];
		
//		amp_effective_mV = 700;//载波限幅为700mV
//		ad603_value = ad603_table[carrier_wave_freq_MHz-30][amp_effective_mV/100 - 1];
		ad603_value = (uint16_t)((float)(y_max-y_min)/(float)(Ma_max-Ma_min)*(float)(Ma_percent-Ma_min) + y_min + 0.5);
	}
	else ad603_value = ad603_table[carrier_wave_freq_MHz-30][amp_effective_mV/100 - 1];
	set_DAC1_value(ad603_value);
}

void set_DAC1_value(uint32_t value){//AD603放大倍数程控，接ADJH
	HAL_DAC_Stop(&hdac, DAC_CHANNEL_1);
	HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value);
	HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
}

void set_DAC2_value(uint32_t value){//调制信号直流偏置，勿动，勿注释，接ADJL
	HAL_DAC_Stop(&hdac, DAC_CHANNEL_2);
	HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
	HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
}

float map_control_voltage_V_for_ad603(char dB){
	return (dB+50.592)/76.4812;//需要实测校正
}

float convert_effective_mV_to_Vpp_mV(float effective_mV){
	return effective_mV*sqrt(2)*2;
}

uint16_t map_Vpp_mV_for_ad9959(float Vpp_mV) {
	#define AD9959_MAX_OUTPUT_VOLTAGE 2828.4271247
	// 确保电压在范围内
	if (Vpp_mV < 0) Vpp_mV = 0;
	else if (Vpp_mV > AD9959_MAX_OUTPUT_VOLTAGE) Vpp_mV = AD9959_MAX_OUTPUT_VOLTAGE;

	// 线性映射到 AD9959 的范围，使用四舍五入
	return (uint16_t)(Vpp_mV / AD9959_MAX_OUTPUT_VOLTAGE * 1023 + 0.5);
}

float convert_tau_ns_to_phi_degree(uint16_t tau_ns, uint16_t freq_MHz){
	float cycle_ns = 1000.0/freq_MHz;
	return 360 - fmod(tau_ns/cycle_ns*360,360);
}

uint16_t map_phase_degree_for_ad9959(float phase_degree){
	// 先将角度归一化到0到360范围
	while (phase_degree < 0) phase_degree += 360;  
	while (phase_degree >= 360) phase_degree -= 360;  

	// 线性映射到AD9959的相位范围
	return (uint16_t)(phase_degree / 360.0 * 16384.0 + 0.5);
}

void ad9959_change_multipath_phase(uint16_t tau_ns, uint16_t phi_degree, uint16_t mod_signal_freq_MHz, uint16_t carrier_wave_freq_MHz){//m(t)=alpha*x(t+tau)*exp(j*phi)
	uint16_t mod_signal_multipath_phase_for_ad9959, carrier_wave_multipath_phase_for_ad9959;
	
	float mod_signal_phase_original = convert_tau_ns_to_phi_degree(tau_ns, mod_signal_freq_MHz) + phi_degree + 9;
	float carrier_wave_phase_original = convert_tau_ns_to_phi_degree(tau_ns, carrier_wave_freq_MHz) + phi_degree + 9;
	
	mod_signal_multipath_phase_for_ad9959 = map_phase_degree_for_ad9959(mod_signal_phase_original);
	carrier_wave_multipath_phase_for_ad9959 = map_phase_degree_for_ad9959(carrier_wave_phase_original);
	ad9959_write_phase(AD9959_CHANNEL_0, 0);
	ad9959_write_phase(AD9959_CHANNEL_1, 0);
	ad9959_write_phase(AD9959_CHANNEL_3, mod_signal_multipath_phase_for_ad9959);
	ad9959_write_phase(AD9959_CHANNEL_2, carrier_wave_multipath_phase_for_ad9959);
}
